<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改排班申请')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: datetimepicker-css" />
<!--    <th:block th:include="include :: datetimepicker-js" />-->
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-application-edit" th:object="${tmdDutyApplication}">
        <input name="id" th:value="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">编号：</label>
            <div class="col-sm-8">
                <input name="id" th:value="*{id}" class="form-control" type="text" disabled>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">排班名称：</label>
            <div class="col-sm-8">
                <input name="arrangeName" th:value="*{arrangeName}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">创建人：</label>
            <div class="col-sm-8">
                <input name="createPerson" th:value="*{createPerson}" class="form-control" type="text" disabled>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">创建时间：</label>
            <div class="col-sm-8">
                <input name="createTime" th:value="*{#dates.format(createTime, 'yyyy-MM-dd HH:mm:ss')}" class="form-control" type="text" disabled>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">紧急程度：</label>
            <div class="col-sm-8">
                <select id="levels" name="level" class="form-control m-b" th:with="type=${@dict.getType('tmd_duty_level')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${dict.dictValue}==*{level}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">部门：</label>
            <div class="col-sm-8">
                <select id="deptId" name="deptId" class="form-control">
                    <option th:text="*{deptName}" th:value="*{deptId}" th:selected="true"></option>
                </select>
            </div>
        </div>
        <table style="margin: 0 auto">
            <tr th:each="item,info:${tmdDutyDatePeopleList}" th:object="${item}">
                <td>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">日期：</label>
                        <div class="col-sm-8">
                            <div class="input-group date" >
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                <input type="text" id="dutyDate" class="form-control laydate-demo-2" name="dutyDate" placeholder="--请选择日期--"  th:value="*{#dates.format(dutyDate, 'yyyy-MM-dd')}">
                            </div>
                        </div>
                    </div>
                </td>
                <td >
                    <div class="form-group"  style="width: 326px">
                        <label class="col-sm-3 control-label"  style="width: 82px">值班人：</label>
                        <div class="col-sm-8">
                            <select class="form-control dutyPerson" name="dutyPersonId" >
                                <option th:text="*{userName}" th:value="*{dutyPersonId}" th:selected="true"></option>
                            </select>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea name="notes" class="form-control">[[*{notes}]]</textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">附件：</label>
            <div class="col-sm-8">
                <input type="hidden" name="file" th:value="*{file}">
                <div class="file-loading">
                    <input class="form-control file-upload" id="file" name="file" type="file">
                </div>
            </div>
        </div>
    </form>
</div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/application";
        $("#form-application-edit").validate({
            // jquery校验传值是否为空
            rules:{
                deptId:{
                    required: true,
                },
                dutyPersonId:{
                    required: true,
                }
            },
            messages:{
                deptId:{
                    required: "请重新选择部门",
                },
                dutyPersonId:{
                    required: "请选择值班人",
                }
            },
            focusCleanup: true,
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-application-edit').serialize());
            }
        }

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });


    </script>
    <script th:inline="javascript">
        $(document).ready(function() {//页面加载时运行此函数 放在最前端
            $.ajax({
                url: ctx + "system/dept/getDeptList",
                type: 'GET',
                success:function (data) {
                    <!--清空下拉框中的缓存-->
                    // $("#deptId").empty();
                    <!--避免下拉框的值不变-->
                    $("#deptId").append("<option value='' disabled>--请选择部门--</option>");
                    <!--使用循环解析后端传来的数据，并用使用js动态拼接html语言-->
                    for (var i = 0; i < data.length; i++){
                        $("#deptId").append("<option value='" + data[i].deptId + "'>" + data[i].deptName + "</option>");
                    }
                }
            });
            $("#deptId").change(function () {//同上面一样
                // alert($("#deptId").val())
                $.ajax({
                    url: ctx + "system/user/getAllUserByDeptId",
                    type: 'GET',
                    data: {
                        //注意：此处传递的值的名字id必须和后端接受的相同，否则接收不到
                        id: $("#deptId").val(),
                    },
                    success:function (data) {
                        $(".dutyPerson").empty();
                        $(".dutyPerson").append("<option value=''>--请选择值班人--</option>");
                        for (var i = 0; i < data.length; i++){
                            $(".dutyPerson").append("<option value='" + data[i].userId + "'>" + data[i].userName + "</option>");
                        }
                    }
                });
            });
        });

        // $("input[name='dutyDate']").datetimepicker({
        //     format: "yyyy-mm-dd",
        //     minView: "month",
        //     autoclose: true
        // });

        $(function(){
            <!-- laydate示例 -->
            layui.use('laydate', function(){
                var laydate = layui.laydate;

                laydate.render({
                    elem: '#dutyDate',
                    type: 'date'
                });
            });
        });

    </script>
</body>
</html>